﻿#region Copyright & licence

// This file is part of Refix.
// 
// Refix is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
// 
// Refix is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with Refix.  If not, see <http://www.gnu.org/licenses/>.
// 
// Copyright (C) 2010-11 David Musgrove and others.

#endregion

using System;
using System.IO;
using System.Linq;

using Refix.Core.IO;
using Refix.Core.Options;
using Refix.Core.Properties;

namespace Refix.Core.Commands
{
    public class Clean : SingleSolutionCommand
    {
        private bool CleanAll
        {
            get { return Options.Options.Any(o => o is All); }
        }

        protected override string HelpText
        {
            get { return ResourceDispenser.GetString("CLEAN_HELP"); }
        }

        public override bool Execute()
        {
            RevertSingleFileFromBackup(FileName);
            var solutionFile = new SolutionFile(FileName);
            bool cleanAll = CleanAll;
            foreach (ProjectFile projectFile in solutionFile.CachedProjectFiles.Values)
            {
                RevertSingleFileFromBackup(projectFile.FileName);
                string directoryName = Path.GetDirectoryName(projectFile.FileName);
                RevertSingleFileFromBackup(Path.Combine(directoryName, "App.config"));
                RevertSingleFileFromBackup(Path.Combine(directoryName, "Web.config"));
                if (cleanAll && File.Exists(projectFile.FileName + FileExtensions.DependencyFile))
                {
                    OutputWriter.WriteLine(OutputVerbosity.Verbose, ResourceDispenser.GetString("CLEAN_DELETING_FILE"),
                                       Path.GetFileName(projectFile.FileName) + FileExtensions.DependencyFile);
                    File.Delete(projectFile.FileName + FileExtensions.DependencyFile);
                }
            }
            string refixFolderName = Path.Combine(Path.GetDirectoryName(solutionFile.FileName), ".refix");
            if (cleanAll && Directory.Exists(refixFolderName))
            {
                OutputWriter.WriteLine(OutputVerbosity.Verbose, ResourceDispenser.GetString("CLEAN_DELETING_FOLDER"),
                                   ".refix");
                Directory.Delete(refixFolderName, true);
            }
            return true;
        }

        static private void RevertSingleFileFromBackup(string fileNameRoot)
        {
            if (File.Exists(fileNameRoot + FileExtensions.BackupFile))
            {
                OutputWriter.WriteLine(OutputVerbosity.Verbose, ResourceDispenser.GetString("CLEAN_REVERTING"),
                                   Path.GetFileName(fileNameRoot));
                File.Delete(fileNameRoot);
                var fileToRestore = new FileInfo(fileNameRoot + FileExtensions.BackupFile);
                if (fileToRestore.Length != 0)
                {
                    fileToRestore.CopyTo(fileNameRoot);
                }
                fileToRestore.Delete();
            }
        }
    }
}